package com.lianniu.mgr.projectMgr.projectInfo.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.serialNo.model.SerialNoVo;
import com.lianniu.mgr.baseService.serialNo.service.ISerialNoService;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.projectMgr.projectInfo.model.ProjectStaffVo;
import com.lianniu.mgr.projectMgr.projectInfo.model.ProjectVo;
import com.lianniu.mgr.projectMgr.projectInfo.service.IProjectService;
import com.lianniu.mgr.projectMgr.projectInfo.service.IProjectStaffService;
import com.lianniu.mgr.util.FileUtil;
import com.lianniu.mgr.util.StringUtil;

@Controller
@RequestMapping("/rest/project")
public class ProjectRestController {	
	private final static Logger logger = Logger.getLogger(ProjectRestController.class);
	
	@Autowired
	private IProjectService projectService;
	
	@Autowired
	private IProjectStaffService projectStaffService;
	
	@Autowired
	private ISerialNoService serialNoService;
		
	/**
	  * @Description:根据项目名称获取项目的分页列表信息
      * @return BaseResult(data=List<ProjectVo>)

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/list"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult list(HttpServletRequest request, PaginationInfo paginationInfo) {
		logger.info("ProjectRestController list begin.");
		String projectName = request.getParameter("projectName");
		logger.info("ProjectRestController list projectName:"+projectName);
		ProjectVo project = new ProjectVo();
		project.setProjectName(projectName);
		project.setPaginationInfo(paginationInfo);
		logger.info("projectService getProjectsByProjectNameForPage begin.");
		BaseResult baseResult = projectService.getProjectsByProjectNameForPage(project);
		logger.info("projectService getProjectsByProjectNameForPage end.");
		logger.info("ProjectRestController list end.");
		return baseResult;
	}
	
	/**
	  * @Description:插入项目记录
	  * @return BaseResult

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/add"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult add(HttpServletRequest request, ProjectVo project) {
		logger.info("ProjectRestController list begin.");
		List<ProjectStaffVo> list = new ArrayList<ProjectStaffVo>();
		String projectId = StringUtil.getUUID();
		String projectStaffId;
		project.setId(projectId);
		project.setDelFlag("1");
		String[] staffNames = request.getParameterValues("staffName");
		if(staffNames != null){
			ProjectStaffVo projectStaffVo;
			String[] postNames = request.getParameterValues("postName");
			String[] staffIds = request.getParameterValues("staffId");
			String[] emails = request.getParameterValues("email");
			String[] phones = request.getParameterValues("phone");
			String[] sexs = request.getParameterValues("sex");
			String[] postTypes = request.getParameterValues("postType");
			String staffName;
			String addUser = project.getAddUser();
			String serialNo;
			BaseResult baseResult;
			for(int i = 0; i < staffNames.length; i++){
				baseResult = serialNoService.getSerialNoId(addUser);
				serialNo = ((SerialNoVo)baseResult.getData()).getId();
				//姓名不能为空，如果为空，此记录无效
				staffName = staffNames[i];
				if(StringUtils.isBlank(staffName)){
					continue;
				}
				projectStaffId = StringUtil.getUUID();
				projectStaffVo = new ProjectStaffVo();
				projectStaffVo.setId(projectStaffId);
				projectStaffVo.setProjectId(projectId);
				// 项目负责人不在以StaffType=3区分，以‘岗位名称 ’postname 包含‘负责人’区分
				//projectStaffVo.setStaffType("3");
				projectStaffVo.setStaffType("1");
				projectStaffVo.setDelFlag("1");
				projectStaffVo.setAddUser(addUser);
				projectStaffVo.setStaffName(staffNames[i]);
				projectStaffVo.setPostName(postNames[i]);
				projectStaffVo.setStaffId(staffIds[i]);
				projectStaffVo.setEmail(emails[i]);
				projectStaffVo.setPhone(phones[i]);
				projectStaffVo.setSex(sexs[i]);
				projectStaffVo.setPostType(postTypes[i]);
				projectStaffVo.setProjectStaffNo(serialNo);
				list.add(projectStaffVo);
			}
		}
		logger.info("ProjectRestController addProject project:"+project);
		logger.info("ProjectRestController addProject list:"+list);
		logger.info("projectService addProject begin.");
		BaseResult baseResult = projectService.addProject(project, list);
		logger.info("projectService addProject end.");
		logger.info("ProjectRestController list end.");
		return baseResult;
	}
	
	/**
	  * @Description:根据id更新项目信息
      * @return BaseResult

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/edit"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult edit(HttpServletRequest request, ProjectVo project) {
		logger.info("ProjectRestController edit begin.");
		List<ProjectStaffVo> list = new ArrayList<ProjectStaffVo>();
		project.setDelFlag("1");
		String projectStaffId;
		String[] staffNames = request.getParameterValues("staffName");
		if(staffNames != null){
			ProjectStaffVo projectStaffVo;
			String[] postNames = request.getParameterValues("postName");
			String[] staffIds = request.getParameterValues("staffId");
			String[] emails = request.getParameterValues("email");
			String[] phones = request.getParameterValues("phone");
			String[] sexs = request.getParameterValues("sex");
			String[] postTypes = request.getParameterValues("postType");
			String staffName;
			String addUser = project.getModifyUser();
			String serialNo;
			BaseResult baseResult;
			for(int i = 0; i < staffNames.length; i++){
				baseResult = serialNoService.getSerialNoId(addUser);
				serialNo = ((SerialNoVo)baseResult.getData()).getId();
				//姓名不能为空，如果为空，此记录无效
				staffName = staffNames[i];
				if(StringUtils.isBlank(staffName)){
					continue;
				}
				projectStaffId = StringUtil.getUUID();
				projectStaffVo = new ProjectStaffVo();
				// 此处不能批量更新，因为有可能会增减记录，id是不全，必须删除在添加
				projectStaffVo.setId(projectStaffId);
				projectStaffVo.setProjectId(project.getId());
				//projectStaffVo.setStaffType("3");
				// 项目负责人不在以StaffType=3区分，以‘岗位名称 ’postname 包含‘负责人’区分
				projectStaffVo.setStaffType("1");
				projectStaffVo.setDelFlag("1");
				projectStaffVo.setAddUser(addUser);
				projectStaffVo.setStaffName(staffNames[i]);
				projectStaffVo.setPostName(postNames[i]);
				projectStaffVo.setStaffId(staffIds[i]);
				projectStaffVo.setEmail(emails[i]);
				projectStaffVo.setPhone(phones[i]);
				projectStaffVo.setSex(sexs[i]);
				projectStaffVo.setPostType(postTypes[i]);
				projectStaffVo.setProjectStaffNo(serialNo);
				list.add(projectStaffVo);
			}
		}
		logger.info("ProjectRestController editProjectById project:"+project);
		logger.info("ProjectRestController editProjectById list:"+list);
		logger.info("projectService editProjectById begin.");
		BaseResult baseResult = projectService.editProjectById(project, list);
		logger.info("projectService editProjectById end.");
		logger.info("ProjectRestController edit end.");
		return baseResult;
	}
	
	/**
	  * @Description:批量删除项目记录
      * @return BaseResult

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/delete"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult delete(HttpServletRequest request) {
		logger.info("ProjectRestController list begin.");
		String id = request.getParameter("ids");
		logger.info("ProjectRestController delete id:"+id);
		List<String> ids = new ArrayList<String>();
		if(!StringUtil.isEmpty(id)){
			String[] idIns = id.split(",");
			for(String idIn : idIns){
				ids.add(idIn);
			}
		}
		logger.info("ProjectRestController delProjectsByIds ids:"+ids);
		logger.info("projectService delProjectsByIds begin.");
		BaseResult baseResult = projectService.delProjectsByIds(ids);
		logger.info("projectService delProjectsByIds end.");
		logger.info("ProjectRestController delete end.");
		return baseResult;
	}
	
	/**
	  * @Description:根据项目id获取项目信息和负责人
      * @return BaseResult(data={project:项目信息， projectStaffList:项目负责人列表 )

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/get"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult get(HttpServletRequest request) {
		logger.info("ProjectRestController get begin.");
		String id = request.getParameter("id");
		logger.info("ProjectRestController get id:"+id);
		ProjectVo project = new ProjectVo();
		project.setId(id);
		Map<String, Object> map = new HashMap<>();
		logger.info("projectService getProjectsByVo begin.");
		BaseResult baseResultProject = projectService.getProjectsByVo(project);
		logger.info("projectService getProjectsByVo end.");
		ProjectStaffVo projectStaff = new ProjectStaffVo();
		projectStaff.setProjectId(id);
		// 根据  PostName like "%负责人%" 区分 项目负责人还是其他人员
		projectStaff.setPostName("负责人");
		logger.info("projectStaffService getProjectStaffsByVo begin.");
		BaseResult baseResult = projectStaffService.getProjectStaffsByVo(projectStaff);
		logger.info("projectStaffService getProjectStaffsByVo end.");
		map.put("project", baseResultProject.getData());
		map.put("projectStaffList", baseResult.getData());
		baseResult.setData(map);
		logger.info("ProjectRestController get end.");
		return baseResult;
	}
	
	/**
	  * @Description:获取项目编号
      * @return BaseResult(data=serialNoVo)

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/getProjectNo"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult getProjectNo(HttpServletRequest request) {
		logger.info("ProjectRestController getProjectNo begin.");
		String userName = request.getParameter("userName");
		logger.info("ProjectRestController getProjectNo userName:"+userName);
		logger.info("serialNoService getId begin.");
		BaseResult baseResult = serialNoService.getSerialNoId(userName);
		logger.info("serialNoService getId end.");
		logger.info("ProjectRestController getProjectNo end.");
		return baseResult;
	}
	
}
